$(document).ready(function() {    
    $('input[name="login"]').click(function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();

        var email    = $('input[name="email"]').val();
        var password = $('input[name="password"]').val();

        var errors = [];
        if (email == '') 
            errors.push('Email is missing');

        if (password == '')
            errors.push('Password is missing');

        if (errors.length > 0) {
            showErrors(errors);
            return;
        }

        $.post('/login/login', {email: email, password: password}, function(r) {
            var resp = JSON.parse(r);
            if (resp.success != 'undefined') 
                location.reload(false);

            $('#login_errors').html('');
            showErrors(resp.errors);
            return false;
        })

        return false;
    })

    var showErrors = function(errors) {
        $('#login_errors').html('');
        for (var i in errors) {
            $('#login_errors').append('<p color="red">' + errors[i] + '</p>');         
        }
    }
})
